﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace FaceSharp.Samples.WinForm
{
    public partial class Authorize : Form
    {
        public Authorize()
        {
            InitializeComponent();

        }

        private void Authorize_Load(object sender, EventArgs e)
        {
            var facebookCore = new FacebookCore();
            var destinationURL = String.Format(
                @"https://www.facebook.com/dialog/oauth?client_id={0}&scope={1}&redirect_uri=http://www.facebook.com/connect/login_success.html&response_type=token",
                facebookCore.ApplicationId,
                facebookCore.ExtendedPermissions);
            webBrowser.Navigated += webBrowser_Navigated;
            webBrowser.Navigate(destinationURL);
        }

        private void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            // get token
            var url = e.Url.Fragment;
            if (url.Contains("access_token") && url.Contains("#"))
            {
                url = (new Regex("#")).Replace(url, "?", 1);
                var facebookCore = new FacebookCore
                                       {
                                           AccessToken =
                                               System.Web.HttpUtility.ParseQueryString(url).Get("access_token")
                                       };
                var photoBrowser = new PhotoBrowser();
                photoBrowser.Show();
                this.Hide();
            }
        }
    }
}
